package com.ynet.middleground.user.service.impl;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.user.bean.*;
import com.ynet.middleground.user.common.ErrorMessageConstant;
import com.ynet.middleground.user.dto.InPortRoleDto;
import com.ynet.middleground.user.dto.OrganRoleRelationshipDto;
import com.ynet.middleground.user.model.RoleManagementModel;
import com.ynet.middleground.user.service.RoleManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import java.util.List;

/**
 * @program: 2019-10-12_userCenter
 * @description: 港内员工角色管理服务实现类
 * @author: DaiGaoLe
 * @create: 2019-10-28 17:33
 **/
@Path("role-management")
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8", MediaType.TEXT_XML + "; " + "charset=UTF-8"})
@Api(value = "港内员工角色管理服务实现类", tags = {"港内员工角色管理服务实现类"})
@Service(timeout = 6000, version = "0.0.1")
public class RoleManagementServiceImpl implements RoleManagementService, GenericService {

    @Autowired
    RoleManagementModel roleManagementModel;

    /* 添加角色 begin */
    @POST
    @Path("add")
    @ApiOperation(value = "港内员工角色添加", notes = "港内员工角色添加")
    @Override
    public ServiceResult<Integer> addRoleInfo(AddRoleInfoReq requestObject) {
        ServiceResult<Integer> resultService = new ServiceResult<>();
        Integer result = null;
        try {
            result = roleManagementModel.addRoleInfo(requestObject);
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                    Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setResult(result);
        return resultService;
    }

    /* 添加角色 end */

    /* 查询角色 begin */
    @POST
    @Path("query")
    @ApiOperation(value = "港内员工角色查询", notes = "港内员工角色查询")
    @Override
    public ServiceResult<List<InPortRoleDto>> queryRoleInfo(QueryRoleInfoReq requestObject) {
        ServiceResult<List<InPortRoleDto>> resultService = new ServiceResult<>();
        List<InPortRoleDto> inPortRoleDtos = null;
        try {
            inPortRoleDtos = roleManagementModel.queryRoleInfo(requestObject);
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                    Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            resultService.setResult(inPortRoleDtos);
            return resultService;
        }
        resultService.setResult(inPortRoleDtos);
        resultService.setPagerInfo(requestObject.getPagerInfo());
        return resultService;
    }

    /* 查询角色 end */

    /* 修改角色 begin */
    @POST
    @Path("change-role")
    @ApiOperation(value = "港内员工角色修改", notes = "港内员工角色修改")
    @Override
    public ServiceResult<String> changeRole(ChangeRoleReq requestObject) {
        ServiceResult<String> resultService = new ServiceResult<>();
        String result = null;
        try {
            result = roleManagementModel.changeRole(requestObject);
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                    Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setResult(result);
        return resultService;
    }
    /* 查询角色 end */

    /* 删除角色 begin */
    @POST
    @Path("delete")
    @ApiOperation(value = "港内员工角色删除", notes = "港内员工角色删除")
    @Override
    public ServiceResult<String> deleteRole(ChangeRoleReq requestObject) {
        ServiceResult<String> resultService = new ServiceResult<>();
        String result = null;
        try {
            result = roleManagementModel.deleteRole(requestObject);
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                    Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setResult(result);
        return resultService;
    }
    /* 删除角色 end */

    /* 员工机构、部门、角色关系维护 begin */
    @POST
    @Path("add-organRole-relationship")
    @ApiOperation(value = "港内员工组织机构、部门、角色关系维护", notes = "港内员工组织机构、部门、角色关系维护，此接口支持添加和删除,当某用户在该机构下不存在任何角色时，自动删除该机构下的该用户")
    @Override
    public ServiceResult<String> addOrganRoleRelationship(AddOrganRoleRelationshipReq requestObject) {
        ServiceResult<String> resultService = new ServiceResult<>();
        String result = null;
        try {
            result = roleManagementModel.addOrganRoleRelationship(requestObject);
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                    Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setResult(result);
        return resultService;
    }
    /* 员工机构、部门、角色关系维护 end */

    /* 员工机构、部门、角色关系查询没有分页信息 begin */
    @POST
    @Path("organ-role-relationship-no-page")
    @ApiOperation(value = "港内员工组织机构、部门、角色关系查询不带分页", notes = "港内员工组织机构、部门、角色关系查询不带分页", hidden = true)
    @Override
    public ServiceResult<List<OrganRoleRelationshipDto>> queryOrganRoleRelationshipNoPage(QueryOrganRoleRelationReq requestObject) {
        ServiceResult<List<OrganRoleRelationshipDto>> resultService = new ServiceResult<>();
        List<OrganRoleRelationshipDto> organRoleRelationshipDtos = null;
        try {
            organRoleRelationshipDtos = roleManagementModel.queryOrganRoleRelationshipReq(requestObject);
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                    Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setResult(organRoleRelationshipDtos);
        return resultService;
    }
    /* 员工机构、部门、角色关系查询没有分页信息 end */
    
    /* 员工机构、部门、角色关系查询 begin */
    @POST
    @Path("organ-role-relationship")
    @ApiOperation(value = "港内员工组织机构、部门、角色关系查询", notes = "港内员工组织机构、部门、角色关系查询")
    @Override
    public ServiceResult<List<OrganRoleRelationshipDto>> queryOrganRoleRelationship(QueryOrganRoleRelationshipReq requestObject) {
        ServiceResult<List<OrganRoleRelationshipDto>> resultService = new ServiceResult<>();
        List<OrganRoleRelationshipDto> organRoleRelationshipDtos = null;
        try {
            organRoleRelationshipDtos = roleManagementModel.queryOrganRoleRelationshipReq(requestObject);
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                    Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setPagerInfo(requestObject.getPagerInfo());
        resultService.setResult(organRoleRelationshipDtos);
        return resultService;
    }
    /* 员工机构、部门、角色关系查询 end */

}
